package com.lancoo.znbkxx.teacher.adapter

import android.os.Build
import android.text.Html
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.model.data.Classical
import com.lancoo.znbkxx.teacher.utils.HtmlUtils
import kotlinx.android.synthetic.main.item_classical.view.*

/**
 * @author : wealive
 * @date   : 2020/3/13 16:20
 * @desc   :
 */
class ClassicalAdapter(layoutResId: Int, data: MutableList<Classical>) :
    BaseQuickAdapter<Classical, BaseViewHolder>(layoutResId, data) {
    var state = 0

    companion object {
        const val TYPE_ADD = 1
        const val TYPE_CLASSICAL = 2
    }

    override fun convert(helper: BaseViewHolder, item: Classical?) {
        with(helper.itemView) {
            if (helper.itemViewType == TYPE_ADD) {
                iv_bg.setBackgroundResource(R.mipmap.item_classical_add)
                if (state == 0) {
                    this.visibility = View.VISIBLE
                } else {
                    tv_classical.visibility = View.GONE
                    iv_delete.visibility = View.GONE
                    this.visibility = View.INVISIBLE
                }
            } else if (helper.itemViewType == TYPE_CLASSICAL) {
                tv_classical.visibility = View.VISIBLE
                iv_bg.setBackgroundResource(R.mipmap.item_classical)
                if (state == 0) {
                    iv_delete.visibility = View.VISIBLE
                    helper.addOnClickListener(R.id.iv_delete)
                } else {
                    iv_delete.visibility = View.GONE
                }
                tv_classical.text =
                    HtmlUtils.setText(mContext.getString(R.string.classic, item!!.StuName))
            }
        }
    }

    override fun getItemViewType(position: Int): Int {
        return if (position == data.size) TYPE_ADD else TYPE_CLASSICAL
    }

    override fun getItemCount(): Int {
        return if (data.size < 3) {
            data.size + 1
        } else {
            data.size
        }
    }

    fun updateState(state: Int) {
        this.state = state
        notifyDataSetChanged()
    }
}